#include "view_temperature.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "Motor.h"
#include "view.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "math.h"
#include "ct1711.h"
#include "body_temperature.h"
#include "blecom.h"
#include "motor.h"
#include "view_temperature.inc"
#include "prompt/view_prompt.h"

void view_temperature_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        switch (view)
        {
        case view_temperature_icon:
            break;
        case view_temperature_detail:
            if (disp_ctrl.auto_refresh_count <= BODY_TEMPERATURE_SECOND)
            {
                disp_ctrl.auto_close_delay = 2000 + DISP_CLOSE_TIME_DEFAULT;
            }
            break;
        }
        float temperature = ct1711_get_temperature();
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        disp_ctrl.com_data.f_temp[25] = get_wrist_temperature();
        disp_ctrl.com_data.f_temp[26] = get_body_temperature();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.com_data.i_temp[0] = get_manual_measure_remain_second();
    }
    draw_on_chip_image(8, 10, icon_temperature);
    // #warning debug
    //     view = view_temperature_icon;

    int const number_width = 8;
    int const number_interval = 1;
    int const number_height = 19;
    switch (view)
    {
    case view_temperature_icon:
    {
        float temperature = disp_ctrl.com_data.f_temp[26];
        bool const is_centigrade = disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C;
        if (!is_centigrade)
        {
            temperature = 32.f + temperature * 1.8f;
        }
        int total_width = 0;
        int const unit_width = 15;
        int const unit_number_interval = 1;
        int const point_width = 3;
        int const point_height = 4;
        int const point_number_interval = 2;
        int x = 0;
        int y = 131;
        // temperature = 132.32f;
        if (isnan(temperature))
        {
            total_width = number_width * 2 + number_interval + unit_number_interval + unit_width;
            x = (OLED_SIZE_X - 1 - total_width) / 2;
            draw_horizontal_line(x, x + number_width - 1, y + number_height / 2 - 1, RGB565_COLOR_WHITE);
            x += number_width + number_interval;
            draw_horizontal_line(x, x + number_width - 1, y + number_height / 2 - 1, RGB565_COLOR_WHITE);
            x += number_width + unit_number_interval;
        }
        else
        {
            int temperature_int = temperature * 100;
            int const number_digit = get_num_digit(temperature_int);
            total_width = unit_width + unit_number_interval + point_number_interval * 2 + point_width + number_digit * number_width + (number_digit - 2) * unit_number_interval;
            x = (OLED_SIZE_X - 1 - total_width) / 2;
            x = draw_on_chip_image_num(temperature_int / 100, x, y, num_8x19_black_TAB, number_width, number_interval, draw_type_normal);
            x += point_number_interval;
            // draw_point(x, y + number_height - 1, RGB565_COLOR_WHITE);
            draw_rectangle(x, y + number_height - point_height - 1, x + point_width - 1, y + number_height - 1 - 1, RGB565_COLOR_WHITE);
            x += point_number_interval + point_height;
            x = draw_on_chip_image_num_with_digits(temperature_int % 100, 2, x, y, num_8x19_black_TAB, number_width, number_interval, draw_type_normal);
            x += unit_number_interval;
        }
        if (is_centigrade)
        {
            draw_on_chip_image(x, y, num_8x19_celsius_black);
        }
        else
        {
            draw_on_chip_image(x, y, num_8x19_fahrenheit_black);
        }
    }
    // disp_show_localize(DISP_LFS_FILE_INDEX_3, localize_string_key_StartBodyTemperatureTips, 150);
    break;
    case view_temperature_detail:
    {
        if (disp_ctrl.auto_refresh_count % 2)
        {
            draw_horizontal_line(30, 30 + 9 - 1, 105, RGB565_COLOR_WHITE);
            draw_horizontal_line(30 + 9 + 1, 30 + 9 + 1 + 9 - 1, 105, RGB565_COLOR_WHITE);
        }
        //        int const unit_width = 6;
        int const unit_number_interval = 1;

        int const unit_height = 14;
        int y = 131;
        int x = draw_on_chip_image_num_center(disp_ctrl.com_data.i_temp[0], 0, y, OLED_SIZE_X, 131, num_8x19_black_TAB, number_width, number_interval, draw_type_normal);
        x += unit_number_interval;
        draw_on_chip_image(x, y + number_height - unit_height, icon_second);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_MeasuringBodyTemperatureTips, 50);
        // disp_draw_user_image_num_filter_color(136, 212, disp_ctrl.com_data.i_temp[0], 12, num_9x23_TAB);
    }
    break;
    }
}

view_event_result_t view_temperature_detail_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        // LOG("view_temperature_event_view_entry");
        start_manual_measure(view_temperature_icon);
        // start_body_measure(BODY_TEMPERATURE_SECOND, true, view_temperature_icon);
        if (param->previous_view == view_prompt_hyperthemia || param->previous_view == view_prompt_not_wear)
        {
            // LOG("view_temperature_event_view_entry");
            display_show(view_temperature_icon, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    case event_view_exit:
        // LOG("view_temperature_event_view_exit");
        // stop_body_measure();
        stop_manual_measure();
        app_cmd_send_ack(SmartBand_temperature_exit, 0);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_temperature_icon, DISP_MODE_NORMAL);
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP:
#endif
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_temperature_icon_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        ct1711_start();
        return view_event_result_handled;
    case event_view_exit:
        ct1711_stop();
        return view_event_result_handled;
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
        display_show(view_temperature_detail, DISP_MODE_NORMAL);
        return view_event_result_handled;
    case BUTTON_EVENT_TP_TAP:
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}
view_event_result_t view_temperature_event(int view, event_param_t const *param)
{
    switch (view)
    {
    case view_temperature_detail:
        return view_temperature_detail_event(param);
    case view_temperature_icon:
        return view_temperature_icon_event(param);
    }
    return view_event_result_not_handled;
}
